

import UIKit
import SnapKit

class VisitorView: UIView {

    // 通过代码创建一个控件就会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 初始化UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // 初始化UI
        setupUI()
    }
    
    // MARK: - 外部控制方法
    func setupVisitorInfo(imageName: String?, title: String)
    {
        // 设置标题
        self.title.text = title
        
        guard let name = imageName else{
            // 首页
            startAnimation()
            return
        }
        // 不是首页
        // 设置中间图标
        icon.image = UIImage(named: name)
        // 隐藏转盘
        homeIcon.hidden = true
    }
    
    // MARK: - 内部控制方法
    /**
    执行转盘旋转动画
    */
    private func startAnimation()
    {
        // 1.创建动画对象
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 10.0
        
        // 设置动画不自动移除, 等到view销毁的时候才移除
        anim.removedOnCompletion = false
        
        // 3.将动画添加到图层上
        homeIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    
    // MARK: - 懒加载
    /// 转盘
    private lazy var homeIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 图标
    private lazy var icon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 文本标签
    private lazy var title: UILabel = {
        let lb = UILabel(color: UIColor.lightGrayColor(), lines: 0)
        lb.text = "放假快乐短时间恢复了开始登记后方可洛杉矶放假快乐短时间"
        return lb
    }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = {
        let btn = UIButton(title: "注册", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
        
        return btn
    }()
    /// 登录按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton(title: "登录", color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
        return btn
    }()
    /// 背景颜色图片
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}

extension VisitorView
{
    
    // MARK: - 内部控制方法
    private func setupUI()
    {
        // 1.添加子控件
        addSubview(homeIcon)
        addSubview(maskImageView)
        addSubview(icon)
        addSubview(title)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.布局子控件
        // 2.1布局转盘
        homeIcon.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 2.2布局图标
        icon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(15)
        }
        // 2.3布局标题
        title.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.top.equalTo(homeIcon.snp_bottom).offset(15)
            make.width.equalTo(225)
        }
        // 2.4布局注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(title)
            make.top.equalTo(title.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        // 2.5布局登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(title)
            make.top.equalTo(title.snp_bottom).offset(15)
            make.width.equalTo(100)
        }
        // 2.6布局蒙版
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton.snp_top)
        }
        
        // 2.7设置view的背景颜色
        backgroundColor = UIColor(white: 235.0/255.0, alpha: 1.0)
    }
}
